/***************************************************************************
 *   Copyright (C) 2008 by Goliash   *
 *   goliash@goliash.net   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "file.h"

File::File()
{
	size = -1;
	pict = NULL;
	audio = NULL;
	document = NULL;
	content = new QByteArray();
}

File::File(QString name)
{
	this->name = name;
	size = -1;
	pict = NULL;
	audio = NULL;
	document = NULL;
	content = new QByteArray();
}

QString File::getName()
{
	return name;
}

bool File::isFolder()
{
	return folder;
}

void File::setFolder(bool folder)
{
	this->folder = folder;
}

QString File::toString()
{
	if(!isFolder())
		return name;
	else {
		if(children.isEmpty())
			return name.append(": Empty\n");
		else {
			QString str;
			str.append(name).append("\n");
			for(int i = 0; i<children.size(); i++) {
				//qDebug() << "child to string";
				str.append("\t").append(children[i]->toString()).append("\n");
			}
			return str;
		}
	}
}

void File::setRights(bool read, bool write, bool exec)
{
	QString r = "-";
	QString w = "-";
	QString x = "-";
	if(read) r = "r";
	if(write) w = "w";
	if(exec) x = "x";
	rights = r+w+x; 
}

void File::setOwners(QString owner, QString group)
{
	this->owner = owner;
	this->group = group;
}

void File::setDates(QDateTime created, QDateTime modified)
{
	this->created = created;
	this->modified = modified;
}

void File::setSize(quint64 size)
{
	this->size = size;
}

void File::setType(int type)
{
	this->type = type;
}

void File::setContent(QByteArray *bytes)
{
	this->content = bytes;
}

void File::setAllContent(bool all)
{
	this->all_content = all;
}

QString File::getRights()
{
	return rights;
}

QDateTime File::getCreatedDate()
{
	return created;
}

QDateTime File::getModifiedDate()
{
	return modified;
}

quint64 File::getSize()
{
	return size;
}

int File::getType()
{
	return type;
}

QByteArray *File::getContent()
{
	return content;
}

bool File::isAllContent()
{
	return all_content;
}

QString File::getFormattedSize(quint64 size, const int format)
{
	QString s;
	switch(format)
	{
		case(ByteF):
			s = QString::number(size) + " B ";
			break;
		case(KiloF):
			s = QString::number((double)size/1024, 'f', 2) + "KB";
			break;
		case(MegaF):
			s = QString::number((double)size/(1024*1024), 'f', 2) + " MB";
			break;
		case(DynamicF):
			if(size < 1024) {
				s = QString::number(size) + " B";
			}
			else if(size < 1024*1024) {
				s = QString::number((double)size/1024, 'f', 2) + " KB";
			}
			else {
				s = QString::number((double)size/(1024*1024), 'f', 2) + " MB";
			}
			break;
		default:
			s = QString::number(size) + " B";
			break;
	}
	return s;
}

QString File::getFormattedDate(QString date, int format)
{
	// input: yyyyMMddhhmmss
	QString d, year, month, day, hour, min, sec;
	year = date.section("", 1, 4);
	month = date.section("", 5, 6);
	day = date.section("", 7, 8);
	hour = date.section("", 9, 10);
	min = date.section("", 11, 12);
	sec = date.section("", 13, 14);
	switch(format)
	{
		case(StandardDateF):
			d = day + "." + month + "." + year + " " + hour + ":" + min + "." + sec;
			break;
		default:
			d = year + "-" + month + "-" + date + " " + hour + ":" + min + "." + sec;
			break;
	}
	return d;
}

Picture *File::getPicture()
{
	if(pict == NULL)
		pict = new Picture();
	return pict;
}

bool File::additionalImageInfo()
{
	if(pict == NULL)
		return false;
	else 
		return true;
}

Audio *File::getAudio()
{
	if(audio == NULL)
		audio = new Audio();
	return audio;
}

bool File::additionalAudioInfo()
{
	if(audio == NULL)
		return false;
	else 
		return true;
}

Document *File::getDocument()
{
	if(document == NULL)
		document = new Document();
	return document;
}

bool File::additionalDocumentInfo()
{
	if(document == NULL)
		return false;
	else 
		return true;
}